package ynu.edu.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.edu.dto.MerchantDTO;
import ynu.edu.entity.Merchant;
import ynu.edu.repository.MerchantRepository;
import ynu.edu.service.MerchantService;

import jakarta.persistence.EntityNotFoundException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商家服务实现类
 */
@Service
@RequiredArgsConstructor
public class MerchantServiceImpl implements MerchantService {
    
    private final MerchantRepository merchantRepository;
    
    /**
     * 获取所有商家
     *
     * @return 商家列表
     */
    @Override
    public List<MerchantDTO> getAllMerchants() {
        return merchantRepository.findAll().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    /**
     * 根据ID获取商家
     *
     * @param businessId 商家ID
     * @return 商家信息
     */
    @Override
    public MerchantDTO getMerchantById(Integer businessId) {
        Merchant merchant = merchantRepository.findById(businessId)
                .orElseThrow(() -> new EntityNotFoundException("未找到ID为" + businessId + "的商家"));
        return convertToDTO(merchant);
    }
    
    /**
     * 根据点餐分类获取商家列表
     *
     * @param orderTypeId 点餐分类ID
     * @return 商家列表
     */
    @Override
    public List<MerchantDTO> getMerchantsByOrderType(Integer orderTypeId) {
        return merchantRepository.findByOrderTypeId(orderTypeId).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    /**
     * 添加商家
     *
     * @param merchantDTO 商家信息
     * @return 添加后的商家信息
     */
    @Override
    @Transactional
    public MerchantDTO addMerchant(MerchantDTO merchantDTO) {
        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(merchantDTO, merchant);
        Merchant savedMerchant = merchantRepository.save(merchant);
        return convertToDTO(savedMerchant);
    }
    
    /**
     * 更新商家
     *
     * @param merchantDTO 商家信息
     * @return 更新后的商家信息
     */
    @Override
    @Transactional
    public MerchantDTO updateMerchant(MerchantDTO merchantDTO) {
        Merchant merchant = merchantRepository.findById(merchantDTO.getBusinessId())
                .orElseThrow(() -> new EntityNotFoundException("未找到ID为" + merchantDTO.getBusinessId() + "的商家"));
        
        BeanUtils.copyProperties(merchantDTO, merchant);
        Merchant updatedMerchant = merchantRepository.save(merchant);
        return convertToDTO(updatedMerchant);
    }
    
    /**
     * 删除商家
     *
     * @param businessId 商家ID
     */
    @Override
    @Transactional
    public void deleteMerchant(Integer businessId) {
        if (!merchantRepository.existsById(businessId)) {
            throw new EntityNotFoundException("未找到ID为" + businessId + "的商家");
        }
        merchantRepository.deleteById(businessId);
    }
    
    /**
     * 搜索商家
     *
     * @param keyword 关键词
     * @return 商家列表
     */
    @Override
    public List<MerchantDTO> searchMerchants(String keyword) {
        return merchantRepository.findByBusinessNameContaining(keyword).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    /**
     * 将实体转换为DTO
     *
     * @param merchant 商家实体
     * @return 商家DTO
     */
    private MerchantDTO convertToDTO(Merchant merchant) {
        MerchantDTO merchantDTO = new MerchantDTO();
        BeanUtils.copyProperties(merchant, merchantDTO);
        return merchantDTO;
    }
} 